using System;
using Jandaya.Framework.Dialogs;

namespace Jandaya.Client
{
	public partial class dlgConfigDataBase : Gtk.Dialog
	{
		public dlgConfigDataBase ()
		{
			this.Build ();
		}

		
		protected override void OnShown ()
		{
			base.OnShown ();
			LoadAppConfig();
		}
		
		private void LoadAppConfig(){
			Jandaya.Framework.Application.DatabaseInfo dbinfo = App.Manager.Config.DBInfo;			
			etrNome.Text = dbinfo.ConnectionName;
			etrServidor.Text = dbinfo.HostName;
			etrBanco.Text = dbinfo.Database;
			etrUsuario.Text = dbinfo.UserName;
			etrSenha.Text = dbinfo.Password;
		}
		
		
		protected virtual void btnTestarClicked (object sender, System.EventArgs e)
		{
			string testLog = string.Empty;
			if (App.Manager.Connections.TestConnection(etrServidor.Text, etrBanco.Text, etrUsuario.Text, etrSenha.Text, out testLog))
				MsgDialog.Show("Conexão estabelecida com sucesso!\n\n"+testLog);
			else
				MsgDialog.ShowError("Não foi possível conectar ao banco!\n\n" + testLog);
		}
		
		protected virtual void btnOkClicked (object sender, System.EventArgs e)
		{
			App.Manager.Config.DBInfo.ConnectionName = etrNome.Text;
			App.Manager.Config.DBInfo.HostName = etrServidor.Text;
			App.Manager.Config.DBInfo.Database = etrBanco.Text;
			App.Manager.Config.DBInfo.UserName = etrUsuario.Text;
			App.Manager.Config.DBInfo.Password = etrSenha.Text;
			App.Manager.Config.Save();
		}
	}
}

